package com.sduept.nwld.dataserver.manager.protection;

import com.sduept.cim.model.core.Substation;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.model.daily.OverhaulMsg;
import com.sduept.utils.DateUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProtectOverhaulManager {
	@Autowired
	SuperDAO dao;
	@Autowired
	private CimManager cm;

	public OverhaulMsg getOverhaulById(String id) {
		return dao.findById(id, OverhaulMsg.class);
	}

	/**
	 * 根据开始时间、结束时间、状态 查询保护检修 （时间查询只对已完工数据有效）
	 * 
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @param State
	 *            状态（String） 待开工、进行中、已完工、全部
	 * @return List<OverhaulMsg> os
	 */
	public List<OverhaulMsg> getOverhaulByTimeAndState(Date start, Date end, String State) {
		List<OverhaulMsg> os = new ArrayList<>();
		if (("进行中").equals(State)) {
			os = dao.findByCondition(OverhaulMsg.class, " done = ? ", "进行中");
		} else if (("待开工").equals(State)) {
			os = dao.findByCondition(OverhaulMsg.class, " done = ? ", "待开工");
		} else {
			if (start != null || end != null) {
				List<Object> values = new ArrayList<>();
				String sql = "endTime >= ? and  endTime <= ? and done = ?";
				values.add(start);
				values.add(end);
				values.add("已完工");
				os = dao.findByCondition(OverhaulMsg.class, sql, values.toArray());
				if (("全部").equals(State)) {
					List<OverhaulMsg> undo = dao.findByCondition(OverhaulMsg.class, " done = ? ", "待开工");
					List<OverhaulMsg> doing = dao.findByCondition(OverhaulMsg.class, " done = ? ", "进行中");
					os.addAll(undo);
					os.addAll(doing);
				}
			}
		}
		return os;
	}

	/**
	 * 选择事件查询方法
	 * 
	 * @param txt
	 * @return
	 */
	public List<OverhaulMsg> getOverhaulMag(String done) {
		List<Object> values = new ArrayList<>();
		String sql = " 1=1 ";
		if (done != null && !(done.equals(""))) {
			sql += "and done = ?";
			values.add(done);
		}
		List<OverhaulMsg> os = dao.findByCondition(OverhaulMsg.class, sql, values.toArray());
		return os;
	}

	/**
	 * 单条编辑
	 * 
	 * @param selected
	 */
	public void editUpdate(OverhaulMsg selected) {
		if (selected != null) {
			dao.update(selected, OverhaulMsg.class);
		}
	}

	/**
	 * @return 所有待开工计划
	 */
	public List<OverhaulMsg> getTodoOverhaulMsg() {
		return dao.findByCondition(OverhaulMsg.class, " done = ? ", "待开工");
	}

	public List<OverhaulMsg> getTodoOverhaulMsg(String stationId) {
		return dao.findByCondition(OverhaulMsg.class, " done = ? and station_id = ?", "待开工", stationId);
	}

	/**
	 * 查询某变电站,开始时间为date当天的待开工数据
	 * 
	 * @param stationId
	 * @param date
	 * @return
	 */
	public List<OverhaulMsg> getTodoOverhaulMsg(String stationId, Date date) {
		if (date == null) {
			throw new NullPointerException();
		}
		String s = DateUtil.getStringDate(date, DateUtil.pattern_ymd);
		Date start = DateUtil.getDateByString(s + " 00:00:00", DateUtil.pattern_ymdhms);

		Date end = DateUtils.addDays(start, 1);
		return dao.findByCondition(OverhaulMsg.class,
				" done = ? and station_id = ? and startTime between ? and ? ", "待开工", stationId, start, end);
	}

	/**
	 * 查询开始时间是date当天的待开工数据
	 * 
	 * @param d
	 * @return
	 */
	public List<OverhaulMsg> getTodoOverhaulMsgByStartDate(Date date) {
		if (date == null) {
			throw new NullPointerException();
		}
		String s = DateUtil.getStringDate(date, DateUtil.pattern_ymd);
		Date start = DateUtil.getDateByString(s + " 00:00:00", DateUtil.pattern_ymdhms);

		Date end = DateUtils.addDays(start, 1);

		return dao.findByCondition(OverhaulMsg.class, " done = ? and startTime between ? and ? ", "待开工",
				start, end);
	}

	/**
	 * @return 所有进行中计划
	 */
	public List<OverhaulMsg> getDoingOverhaulMsg() {
		return dao.findByCondition(OverhaulMsg.class, " done = ? ", "进行中");
	}

	public List<OverhaulMsg> getDoingOverhaulMsg(String stationId) {
		return dao.findByCondition(OverhaulMsg.class, " done = ? and station_id = ?", "进行中", stationId);
	}

	/**
	 * @return 按计划完成时间时间查询 已完成计划
	 */
	public List<OverhaulMsg> getDoneOverhaulMsgByDate(Date start, Date end) {
		return dao.findByCondition(OverhaulMsg.class, " done = ? and endTime between ? and ? ", "已完工", start,
				end);
	}

	public List<OverhaulMsg> getDoneOverhaulMsgByDate(Date start, Date end, String stationId) {
		return dao.findByCondition(OverhaulMsg.class,
				" done = ? and station_id = ? and endTime between ? and ?  ", "已完工", stationId, start, end);
	}

	/**
	 * 单条删除
	 */
	public void delOverhaulMag(OverhaulMsg selectOverhaul) {
		if (selectOverhaul != null) {
			try {
				dao.delete(selectOverhaul.getId(), OverhaulMsg.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 数据更新
	 */
	public OverhaulMsg updateOverhaulMag(OverhaulMsg selectOverhaul) {
		if (selectOverhaul != null) {
			updateOverhaulStationId(selectOverhaul);
			try {
				return dao.update(selectOverhaul, OverhaulMsg.class); // update新的数据到这条Overhaulmag
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public OverhaulMsg updateOverhaulStationId(OverhaulMsg m) {
		if (m.getStation_name() != null) {
			List<Substation> s = cm.getStationByName(m.getStation_name());
			if (s.size() > 0 && s.get(0) != null) {
				m.setStation_id(s.get(0).getId());
			} else {
				m.setStation_id(null);
			}
		} else {
			m.setStation_id(null);
		}
		return m;
	}

	/**
	 * 新增数据
	 * 
	 * @return
	 */
	public OverhaulMsg creatOverhaulMag(OverhaulMsg created) {
		if (created != null) {
			updateOverhaulStationId(created);
			try {
				created = dao.create(created, OverhaulMsg.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return created;
	}

	/**
	 * 通过改变开始时间和结束时间，改变检修状态和执行情况
	 * 
	 * @param msg
	 * @return
	 */
	public OverhaulMsg updateOverhaulMsgBySatus(OverhaulMsg msg) {
		String nowDay = DateUtil.getStringDate(new Date(), DateUtil.pattern_ymd);
		nowDay = nowDay + " 00:00:00";
		Date nowDate = DateUtil.getDateByString(nowDay, DateUtil.pattern_ymdhms);
		if (msg.getStartTime() != null && msg.getStartTime().getTime() >= nowDate.getTime()) {
			msg.setDone("待开工");
			msg.setExecutiveCondition("");
		} else if (msg.getStartTime() != null && msg.getStartTime().getTime() < nowDate.getTime()
				&& msg.getEndTime() != null && msg.getEndTime().getTime() >= nowDate.getTime()) {
			msg.setDone("进行中");
			msg.setExecutiveCondition("工作暂未发现异常");

		} else if (msg.getEndTime() != null && msg.getEndTime().getTime() < nowDate.getTime()) {
			msg.setDone("已完工");
			if (msg.getContent() != null) {
				if (msg.getContent().contains("消缺") || msg.getContent().contains("缺陷处理")) {
					msg.setExecutiveCondition("经消缺，装置恢复正常状态");
				} else {
					msg.setExecutiveCondition("工作中未见异常");
				}
			}
		}
		return msg;
	}

	public List<OverhaulMsg> findOverhaulGroupByApplicantCompany() {
		return dao.findByCondition(OverhaulMsg.class, "1=1 group by applicantCompany");
	}
}
